# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>

_realname=libtiff
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=4.3.0
pkgrel=7
pkgdesc="Library for manipulation of TIFF images (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://libtiff.gitlab.io/libtiff/"
license=(MIT)
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-jbigkit"
         "${MINGW_PACKAGE_PREFIX}-lerc"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libdeflate"
         $( [[ ${MINGW_PACKAGE_PREFIX} == *-clang-aarch64* ]] || \
            echo "${MINGW_PACKAGE_PREFIX}-libwebp" )
         "${MINGW_PACKAGE_PREFIX}-xz"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc" "${MINGW_PACKAGE_PREFIX}-autotools")
options=('staticlibs' 'strip')
source=(https://download.osgeo.org/libtiff/tiff-${pkgver}.tar.gz
        "0001-fix-test-newline.patch"
        "0002-fix-static-library.patch")
sha256sums=('0e46e5acb087ce7d1ac53cf4f56a09b221537fc86dfc5daaad1c2e89e1b37ac8'
            'aacda67800bd9c4704761519876631c7309021d37594d72165361386547e9359'
            '6d54a445085c65b0e3cad5d11c18415fcd306ac191cbd52c56240e87868e38b3')

prepare() {
  cd tiff-${pkgver}
  patch -p0 -i "${srcdir}/0001-fix-test-newline.patch"

  # https://gitlab.com/libtiff/libtiff/-/merge_requests/286
  patch -p1 -i "${srcdir}/0002-fix-static-library.patch"

  # autoreconf to get updated libtool files for clang support
  autoreconf -fiv
}

build() {
  export CFLAGS+=" -fno-strict-aliasing"
  export CXXFLAGS+=" -fno-strict-aliasing"

  [[ -d "${srcdir}/build-${MSYSTEM}-static" ]] && rm -rf "${srcdir}/build-${MSYSTEM}-static"
  mkdir -p "${srcdir}/build-${MSYSTEM}-static" && cd "${srcdir}/build-${MSYSTEM}-static"

  ../tiff-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --enable-static \
    --disable-shared \
    --enable-cxx \
    --enable-jbig \
    --enable-lerc \
    --enable-libdeflate \
    --enable-webp \
    --without-x

  make

  [[ -d "${srcdir}/build-${MSYSTEM}-shared" ]] && rm -rf "${srcdir}/build-${MSYSTEM}-shared"
  mkdir -p "${srcdir}/build-${MSYSTEM}-shared" && cd "${srcdir}/build-${MSYSTEM}-shared"

  ../tiff-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --disable-static \
    --enable-shared \
    --enable-cxx \
    --enable-jbig \
    --enable-lerc \
    --enable-libdeflate \
    --enable-webp \
    --without-x

  make
}

check() {
  cd "${srcdir}/build-${MSYSTEM}-static"
  make check || true

  cd "${srcdir}/build-${MSYSTEM}-shared"
  make check || true
}

package() {
  cd "${srcdir}/build-${MSYSTEM}-static"
  make DESTDIR="${pkgdir}" install

  cd "${srcdir}/build-${MSYSTEM}-shared"
  make DESTDIR="${pkgdir}" install

  cp ${srcdir}/tiff-${pkgver}/libtiff/{tiffiop,tif_dir}.h "${pkgdir}${MINGW_PREFIX}/include/"
  cp "libtiff/tif_config.h" "${pkgdir}${MINGW_PREFIX}/include/"

  # License
  # See https://fedoraproject.org/wiki/Licensing:MIT#Hylafax_Variant
  install -Dm644 "${srcdir}/tiff-${pkgver}/COPYRIGHT" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYRIGHT"
}
